package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodLibraryDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.FoodLibrary;
import com.itheima.service.FoodLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = FoodLibraryService.class)
@Transactional
public class FoodLibraryServiceImpl implements FoodLibraryService {

    @Autowired
    private FoodLibraryDao foodLibraryDao;
    @Override
    public void add(List<FoodLibrary> foodLibraryList) {

        if (foodLibraryList!=null &&foodLibraryList.size()>0){
            for (FoodLibrary foodLibrary : foodLibraryList) {
                String name = foodLibraryDao.findFoodName(foodLibrary.getFoodname());
                if (name==null){
                    foodLibraryDao.add(foodLibrary);
                }
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> pageInfo = foodLibraryDao.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getResult());
        return pageResult;
    }
    @Override
    public List<FoodLibrary> findAll() {
        return foodLibraryDao.findAll();
    }

    @Override
    public void insert(FoodLibrary foodLibrary) {
        foodLibraryDao.insert(foodLibrary);

    }
}
